import { PostId, Post } from './model';
import { UserId } from '../user/model';
import { createPostRepository } from './repository';

const postRepository = createPostRepository();

export async function getPost(postId: PostId): Promise<Post | undefined> {
  return await postRepository.selectByPostId(postId);
}

export async function getUserPost(userId: UserId): Promise<Post[]> {
  return await postRepository.selectByUserId(userId);
}

export type CreatePostData = Omit<Post, 'postId'>;

export async function create(post: CreatePostData): Promise<PostId> {
  return await postRepository.insert({ postId: -1, ...post });
}

export async function update(post: Post): Promise<void> {
  await postRepository.update({ ...post });
}
